package com.easyoffer.sentinel;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easyoffer.common.BaseResponse;
import com.easyoffer.common.PageRequest;
import com.easyoffer.common.util.ResultUtils;
import com.easyoffer.model.vo.QuestionBankVO;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class BlockingHandler {
    public static BaseResponse<Page<QuestionBankVO>> listQuestionBankPageBlockHandler(PageRequest pageRequest,
                                                                                      HttpServletRequest request, BlockException ex){
        if (ex instanceof DegradeException)
            return FallbackHandler.listQuestionBankPageFallHandler(pageRequest,request,ex);
        if (ex instanceof ParamFlowException)
            return listQuestionBankPageParamBlockHandler(pageRequest,request,ex);
        return ResultUtils.error(500,"系统繁忙，请稍后再试");
    }

    public static BaseResponse<Page<QuestionBankVO>> listQuestionBankPageParamBlockHandler(PageRequest pageRequest,
                  HttpServletRequest request, BlockException ex){
        return ResultUtils.error(500,"访问过于频繁，请稍后再试");
    }

}
